package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.MessageConstant;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.BlogSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类相关接口
 */

@RestController
@RequestMapping("/classify")
@Api(tags = "分类相关接口")
public class WebClassifyController {

    @Autowired
    BlogSortService blogSortService;

    /**
     *通过blogSortUid获取文章
     * @param blogSortUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation("通过blogSortUid获取文章")
    @GetMapping("/getArticleByBlogSortUid/{blogSortUid}/{currentPage}/{pageSize}")
    public String getArticleByBlogSortUid(@PathVariable("blogSortUid") String blogSortUid,@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize){

        if (blogSortUid==null || "".equals(blogSortUid)){
            return ResultUtil.result(401, MessageConstant.MISSING_KEY_INFORMATION);
        }

        try {
            Page<Blog> articleByBlogSortUid = blogSortService.getArticleByBlogSortUid(blogSortUid, currentPage, pageSize);
            return ResultUtil.result(200, articleByBlogSortUid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.GET_BLOGSORT_FAIL);
        }
    }


    /**
     * 获取分类信息
     * @return
     */
    @PostMapping("/getBlogSortList")
    @ApiOperation("获取分类信息")
    public String getBlogSortList(){


        try {
            BlogSortVO blogSortVO = new BlogSortVO();
            blogSortVO.setCurrentPage(1);
            blogSortVO.setPageSize(10);
            List<BlogSort> blogSortList= blogSortService.getList(blogSortVO);
            return ResultUtil.result(HttpServletResponse.SC_OK,blogSortList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.BLOGSORT_FINDALL_FAIL);
        }
    }


}
